module.exports = function(grunt){
    
    var TrsToDis = function(trs, dis, json){
        var result = {};
        for(var m in json){
            for(var o in json[m]){
                var module = dis + m + "/" + o;
                result[module] = [];
                for(var i = 0, len = json[m][o].length; i < len; i++){
                    result[module].push(trs + m + "/" + json[m][o][i])
                }
            }  
        }
        return result;
    };
    
    // 项目配置
    grunt.initConfig({
        pkg: grunt.file.readJSON('json/package.json'),
        pub: {
            dis_js: "public/dist/js/",
            src_trs: "public/src/js/.transport/"
        },
        uglify: {  //合并压缩
            options: {
                mangle: false, //不重置变量名
                //preserveComments: 'all', //不删除注释，还可以为 false（删除全部注释），some（保留@preserve @license @cc_on等注释）
                banner:'/* 最后修改于：<%= grunt.template.today("yyyy-mm-dd") %>\n*/'//添加footer
            },
            transport: {  //转化为transport的格式
                options:{
                    report: "gzip"//输出压缩率，可选的值有 false(不输出信息)，gzip
                }, 
                /* "模块名":{ "目的文件名" : ["文件"...]}  注：目的文件名必须和数组的入口文件同名*/
                files: TrsToDis("<%= pub.src_trs %>", "<%= pub.dis_js %>", grunt.file.readJSON('json/uglify_transport.json'))
            }
        },

        compass: {          
            development: {   
              options: {           
                sassDir: 'public/src/sass',
                cssDir: 'public/dist/css',
                environment: 'production' //Can be: development[未压缩，开发调试使用 ] or production[已压缩]
              }
            }
        }, 

        jshint: {  //对于正在编写的文件进行检查
            //all: ["public/dist/**/*.js"]  //对未压缩的进行检查，已压缩的看效果检测
        },

        transport: {
            options:{
                debug: false
            },
            target_name: {
                options:{
                    idleading: "./public/dist/js/",  //transport后id路径前缀
                },
                files: [{
                    expand: true,
                    cwd: 'public/src/js',
                    src: '**/*.js',
                    dest: 'public/src/js/.transport'  //transport格式
                }]
            }
        },

        clean:{
            transport: ['public/src/js/.transport']
        },

        watch: {
            files: ['public/src/js/*/**'],
            tasks: ['compass','jshint']  //有顺序
        },
    });

    // 加载插件
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-cmd-transport');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-compass');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-clean');
    
    // 默认任务
    //发布时操作：seajs：transport -> uglify & concat -> clean
    grunt.registerTask('default', ['transport', 'uglify', 'clean']);
};